package com.courage.crawler.boss.service.crawler;

import com.alibaba.fastjson.JSON;
import com.courage.crawler.boss.domain.mapper.TBossJobMapper;
import com.courage.crawler.boss.domain.po.TBossJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class RenderCrawlerRule implements CrawlerRule {

    @Autowired
    private TBossJobMapper tBossJobMapper;

    @Override
    public void doAction(ChromeDriverContext chromeDriverContext) throws InterruptedException {
        ChromeDriver driver = chromeDriverContext.getChromeDriver();

//        try {
//            //判断是否有浮窗 div login-dialog-wrapper span:icon-close
//            WebElement loginDiv = driver.findElement(new By.ByClassName(("login-dialog-wrapper")));
//            if (loginDiv != null) {
//                log.info("关闭login登录窗口");
//                WebElement closeSpan = driver.findElement(new By.ByClassName(("icon-close")));
//                closeSpan.click();
//            }
//        } catch (Exception e) {
//            log.error("login div error:", e);
//        }

        //下拉到底部
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight - 400)");
        List<WebElement> list = driver.findElements(By.className("job-card-wrapper"));

        //jobs
        for (WebElement webElement : list) {
            String jobName = webElement.findElement(By.className("job-name")).getText();
            String jobArea = webElement.findElement(By.className("job-area")).getText();
            String salary = webElement.findElement(By.className("salary")).getText();
            String companyName = webElement.findElement(By.className("company-name")).getText();
            String jobUrl = webElement.findElement(By.className("job-card-left")).getAttribute("href");
            if (StringUtils.isNoneBlank(jobUrl)) {
                int first = jobUrl.lastIndexOf("?");
                jobUrl = jobUrl.substring(0, first);
            }
            TBossJob tBossJob = new TBossJob();
            tBossJob.setCompanyName(companyName);
            tBossJob.setJobArea(jobArea);
            tBossJob.setSalary(salary);
            tBossJob.setJobName(jobName);
            tBossJob.setCreateTime(new Date());
            tBossJob.setUpdateTime(new Date());
            tBossJob.setSalary(salary);
            tBossJob.setJobId(jobUrl);
          //  System.out.println(JSON.toJSONString(tBossJob));
            if (tBossJobMapper.countByJobId(jobUrl) == 0) {
                tBossJobMapper.insertSelective(tBossJob);
            } else {
                log.warn("jobUrl:" + jobUrl + "已存在");
            }
        }
    }

    public static void main(String[] args) {
        String s = "/jtob_detail/3f8a524d31bf94cb1XB80tS_GVVT.html?lid=4X2EKOPok0S.search.29&amp;securityId=l7QhJTKjIxi6g-G1fcE7RoxV70w0phK2HLI5924geBog1M9oakaDntO5WiUreuykmavqigFKxngEpUoReaa-o_JU0LMkIiIYKQqONgLEuZ_r7HZ2g~~";
        int first = s.lastIndexOf("?");
        String r = s.substring(0, first);
        System.out.println(r);
    }

}
